<template>
  <div>
    <!-- 添加图书的表单 -->
    <el-form :inline="true" :model="newBook" class="demo-form-inline" style="margin-bottom: 20px;">
      <el-form-item label="标题">
        <el-input v-model="newBook.title" placeholder="图书标题"></el-input>
      </el-form-item>
      <el-form-item label="作者">
        <el-input v-model="newBook.author" placeholder="作者"></el-input>
      </el-form-item>
      <el-form-item label="副本数量">
        <el-input-number v-model="newBook.copies" :min="1"></el-input-number>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="addBook">添加图书</el-button>
      </el-form-item>
    </el-form>

    <!-- 图书表格 -->
    <el-table
      :data="tableData"
      style="width: 100%"
      border>
      <el-table-column
        prop="id"
        label="ID"
        width="80">
      </el-table-column>
      <el-table-column
        prop="title"
        label="标题"
        width="180">
      </el-table-column>
      <el-table-column
        prop="author"
        label="作者"
        width="180">
      </el-table-column>
      <el-table-column
        prop="copies"
        label="副本数量">
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
  export default {
    data() {
      return {
        tableData: [
          {"id": 1, "title": "Python编程", "author": "John Doe", "copies": 4},
          {"id": 2, "title": "历史的秘密", "author": "Jane Smith", "copies": 3},
          {"id": 3, "title": "宇宙探索", "author": "Emily Brown", "copies": 2},
          {"id": 4, "title": "健康生活", "author": "Mike Johnson", "copies": 5},
          {"id": 5, "title": "数学之美", "author": "Sarah Lee", "copies": 4},
          {"id": 6, "title": "艺术欣赏", "author": "David White", "copies": 3},
          {"id": 7, "title": "编程入门", "author": "Alice Green", "copies": 2},
          {"id": 8, "title": "科幻世界", "author": "Frank Black", "copies": 5},
          {"id": 9, "title": "自然奇观", "author": "Grace Blue", "copies": 4},
          {"id": 10, "title": "音乐之声", "author": "Tom Red", "copies": 3}
        ],
        newBook: {
          title: '',
          author: '',
          copies: 1
        }
      }
    },
    methods: {
      addBook() {
        if (!this.newBook.title || !this.newBook.author) {
          this.$message.error('请填写标题和作者');
          return;
        }
        
        // 生成新ID（当前最大ID+1）
        const newId = this.tableData.length > 0 
          ? Math.max(...this.tableData.map(book => book.id)) + 1 
          : 1;
        
        this.tableData.push({
          id: newId,
          title: this.newBook.title,
          author: this.newBook.author,
          copies: this.newBook.copies
        });
        
        // 重置表单
        this.newBook = {
          title: '',
          author: '',
          copies: 1
        };
        
        this.$message.success('图书添加成功');
      },
      handleDelete(index, row) {
        this.$confirm('确认删除这本书吗?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.tableData.splice(index, 1);
          this.$message({
            type: 'success',
            message: '删除成功!'
          });
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });          
        });
      }
    }
  }
</script>

<style>
  .demo-form-inline {
    margin-top: 20px;
    padding: 20px;
    background: #f5f7fa;
    border-radius: 4px;
  }
</style>